package com.valuegroup.smarttodo.ui.programme.goals;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.data.DataManager;
import com.valuegroup.smarttodo.data.vo.GoalsTraceSourceVo;
import com.valuegroup.smarttodo.plugins.timeline.TimelineView;

import net.cachapa.expandablelayout.ExpandableLayout;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 习惯清单列表
 */
public class GoalsTraceSourceAdapter extends RecyclerView.Adapter {

    private static final String TAG = GoalsTraceSourceAdapter.class.getSimpleName();

    private final Context context;
    private final View view;
    private List<GoalsTraceSourceVo> itemList;
    private RecyclerView recyclerView;

    public GoalsTraceSourceAdapter(Context context, View view, RecyclerView recyclerView, List<GoalsTraceSourceVo> itemList) {
        this.context = context;
        this.view = view;
        this.itemList = itemList;
        this.recyclerView = recyclerView;
    }

    public void addItem(int position, GoalsTraceSourceVo item) {
        itemList.add(position, item);

        // 因添加了头尾布局，所以需要加上顶部
        notifyItemInserted(position + START_COUNT);
    }

    @Override
    public int getItemCount() {
        return itemList.size() + START_COUNT + END_COUNT;
    }

    public int getContentSize() {
        return itemList.size();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        if (viewType == TYPE_HEAD) {
            return new HeadHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_default_header, viewGroup, false));
        } else if (viewType == TYPE_CONTENT) {
            return new ItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_goals_trace_source, viewGroup, false));
        } else {
            return new ButtomHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_default_bottom, viewGroup, false));
        }

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder v = (ItemViewHolder) holder;
            v.initData(itemList.get(position - START_COUNT));
        }
    }

    public final static int START_COUNT = 1;
    public final static int END_COUNT = 1;

    public final static int TYPE_HEAD = 0;
    public final static int TYPE_CONTENT = 1;
    public final static int TYPE_BUTTOM = 2;

    @Override
    public int getItemViewType(int position) {
        int contentSize = getContentSize();
        if (position == 0) {
            return TYPE_HEAD;
        } else if (position == START_COUNT + contentSize) {
            return TYPE_BUTTOM;
        } else {
            return TYPE_CONTENT;
        }
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder implements ExpandableLayout.OnExpansionUpdateListener {

        @BindView(R.id.timeline)
        TimelineView timeline;
        @BindView(R.id.labTitle)
        TextView tvTitleLab;
        @BindView(R.id.labType)
        TextView labType;
        @BindView(R.id.labDate)
        TextView labDate;
        @BindView(R.id.labStatus)
        TextView labStatus;
        @BindView(R.id.layoutRoot)
        LinearLayout layoutRoot;
        @BindView(R.id.btnExpand)
        LinearLayout btnExpand;
        @BindView(R.id.tagExpand)
        ImageView tagExpand;
        @BindView(R.id.expandableLayout)
        ExpandableLayout expandableLayout;
        @BindView(R.id.recyclerChild)
        RecyclerView recyclerChild;

        private GoalsTraceSourceVo data;

        public ItemViewHolder(final View v) {
            super(v);
            ButterKnife.bind(this, v);

            expandableLayout.setInterpolator(new OvershootInterpolator());
            expandableLayout.setOnExpansionUpdateListener(this);

            recyclerChild.setHasFixedSize(true);
            recyclerChild.setLayoutManager(new LinearLayoutManager(v.getContext()));
            recyclerChild.setAdapter(new GoalsTraceSourceBranchAdapter(DataManager.getBranchData()));
        }

        @OnClick({R.id.layoutRoot, R.id.btnExpand})
        public void onViewClicked(View v) {
            switch (v.getId()) {
                case R.id.btnExpand:
                    if (expandableLayout.isExpanded()) {
                        tagExpand.setImageResource(R.drawable.ic_expand_gray3);
                        expandableLayout.collapse();
                    } else {
                        expandableLayout.expand();
                        tagExpand.setImageResource(R.drawable.ic_shrink_gray3);

                    }
                    break;
            }
        }

        public void initData(GoalsTraceSourceVo data) {
            this.data = data;
            //String subTime = String.format(context.getResources().getString(R.string.habit_sub_time), data.getConsuming());
            tvTitleLab.setText(data.getTitle());
            labDate.setText(data.getDate());
            labStatus.setText(data.getStatus());
            labType.setText(data.getType());
            timeline.setMarker(VectorDrawableCompat.create(itemView.getResources(), R.drawable.ic_marker_inactive, itemView.getContext().getTheme()), R.color.colorLink);

        }

        @Override
        public void onExpansionUpdate(float expansionFraction, int state) {
            if (state == ExpandableLayout.State.EXPANDING) {
                //平滑到你想显示的项
                recyclerView.smoothScrollToPosition(getAdapterPosition());
            }
        }
    }

    // 头部内容
    public class HeadHolder extends RecyclerView.ViewHolder {
        public HeadHolder(View itemView) {
            super(itemView);
        }
    }

    // 尾部内容
    public class ButtomHolder extends RecyclerView.ViewHolder {
        public ButtomHolder(View itemView) {
            super(itemView);
        }
    }
}
